﻿Imports System.IO

Public Class exportpdf
	Inherits System.Web.UI.Page

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			If String.IsNullOrEmpty(Me.Request.QueryString("rptname")) Then
				Me.Response.Write("ค่าที่ใช้ในการแสดง PDF ไม่ถูกต้อง")
			Else
				If Me.Session("reportdatasource") Is Nothing Then
					Me.Response.Write("ข้อมูลที่ใช้ในการแสดง PDF ไม่ถูกต้อง")
				Else
					ExportPDF(Me.Request.QueryString("rptname"))
				End If
			End If
		End If
	End Sub

	Private Sub ExportPDF(ByVal rptName As String)
		Dim rptPath As String = String.Empty
		Dim dtSource As DataTable = Me.Session("reportdatasource")
		Select Case rptName
			Case "ut203"
				rptPath = "~/report/UT203.rpt"
			Case "ut502"
				rptPath = "~/report/UT502.rpt"
			Case "ut503"
				rptPath = "~/report/UT503.rpt"
			Case "ut504"
				rptPath = "~/report/UT504.rpt"
			Case "ut505"
				rptPath = "~/report/UT505.rpt"
			Case "ut506"
				rptPath = "~/report/UT506.rpt"
			Case "ut508"
				rptPath = "~/report/UT508.rpt"
			Case "ut509"
				rptPath = "~/report/UT509.rpt"
			Case "ut510"
				rptPath = "~/report/ut510.rpt"
			Case "ut511"
				rptPath = "~/report/ut511.rpt"
			Case "ut512"
				rptPath = "~/report/ut512.rpt"
			Case "ut515"
				rptPath = "~/report/ut515.rpt"
			Case "ut516"
				rptPath = "~/report/ut516.rpt"
			Case "ut518"
				rptPath = "~/report/ut518.rpt"
			Case Else
				Me.Response.Write("ไม่พบไฟล์ report ที่ท่านต้องการแสดง")
				Exit Sub
		End Select

		Dim crySource As New CrystalDecisions.Web.CrystalReportSource
		crySource.Report.FileName = Server.MapPath(rptPath)
		crySource.ReportDocument.SetDataSource(dtSource)
		SetParameter(rptName, crySource)

		Dim strm As Stream
		strm = crySource.ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
		Dim intLength As Integer = Integer.Parse(strm.Length.ToString)
		Dim bytBuffer(intLength) As Byte
		strm.Read(bytBuffer, 0, intLength)
		strm.Flush()
		Response.ClearContent()
		Response.ClearHeaders()
		Response.Buffer = True
		Response.AddHeader("Content-Disposition", "inline; filename=rnwreport.pdf")
		Response.AddHeader("Content-Length", intLength.ToString)
		Response.ContentType = "application/pdf"
		Response.OutputStream.Write(bytBuffer, 0, intLength)
		Response.Flush()
		Response.Close()
		strm.Close()
		strm = Nothing

		Me.Session.Remove("reportdatasource")
		Me.Session.Remove("reportparameter")
	End Sub

	Public Sub SetParameter(ByVal rptName As String, ByVal repSource As CrystalDecisions.Web.CrystalReportSource)
		Select Case rptName
			Case "ut502", "ut503", "ut504", "ut505", "ut515", "ut516", "ut508", "ut509", "ut518"
			Case Else
				Exit Sub
		End Select

		If Me.Session("reportparameter") IsNot Nothing Then
			Dim lstParam As List(Of String) = Me.Session("reportparameter")
			Dim i As Integer = 0

			For Each Str As String In lstParam
				repSource.ReportDocument.SetParameterValue(i, Str)
				i += 1
			Next
		End If
	End Sub
End Class